﻿#region zh-CHS 2006 - 2007 DemoSoft 团队 | en 2006 - 2007 DemoSoft Team

//     NOTES
// ---------------
//
// This file is a part of the G.O.S.E(Game Online Server Engine) for .NET.
//
//                 2006-2007 DemoSoft Team
//
//
// First Version : by H.Q.Cai - mailto:caihuanqing@hotmail.com

/***************************************************************************
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published
 *   by the Free Software Foundation; either version 2.1 of the License, or
 *   (at your option) any later version.
 *
 ***************************************************************************/

#region zh-CHS 包含名字空间 | en Include namespace
using System;
using System.Threading;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Demo_G.O.S.E.ServerEngine.Common;
#endregion

namespace Demo_G.O.S.E.ServerEngine.Network.DLL
{
    /// <summary>
    /// (内存池)难得用一次泛型,C# 比 C++ 泛型好用好写多了 :)
    /// </summary>
    /// <typeparam name="T"></typeparam>
    [MultiThreadedSupportgAttribute( "当前的类所有成员都可锁定,支持多线程操作" )]
    internal class ProcessMessageBlockPool<T> where T : new()
    {
        #region zh-CHS 私有成员变量 | en Private Member Variables
        /// <summary>
        /// 内存池的名字
        /// </summary>
        private string m_Name = string.Empty;
        /// <summary>
        /// 内存池的容量
        /// </summary>
        private int m_InitialCapacity = 0;
        /// <summary>
        /// 内存池的容量不足时再次请求数据的次数
        /// </summary>
        private int m_Misses = 0;
        /// <summary>
        /// 内存池
        /// </summary>
        private Queue<T> m_FreePool = null;
        /// <summary>
        /// 内存池锁
        /// </summary>
        private object m_LockFreePool = new object();
        #endregion

        #region zh-CHS 构造和初始化和清理 | en Constructors and Initializers and Dispose
        /// <summary>
        /// 初始化内存池
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="iInitialCapacity"></param>
        public ProcessMessageBlockPool( string strName, int iInitialCapacity )
        {
            m_Name = strName;

            m_InitialCapacity = iInitialCapacity;

            m_FreePool = new Queue<T>( iInitialCapacity );

            for ( int iIndex = 0; iIndex < iInitialCapacity; ++iIndex )
                m_FreePool.Enqueue( new T() );
        }
        #endregion

        #region zh-CHS 共有方法 | en Public Methods
        /// <summary>
        /// 内存池请求数据
        /// </summary>
        /// <returns></returns>
        public T AcquirePoolContent()
        {
            T l_Return = default( T );

            Monitor.Enter( m_LockFreePool );
            do
            {
                if ( m_FreePool.Count > 0 )
                {
                    l_Return = m_FreePool.Dequeue();
                    break;
                }

                ++m_Misses;

                for ( int iIndex = 0; iIndex < m_InitialCapacity; ++iIndex )
                    m_FreePool.Enqueue( new T() );

                l_Return = m_FreePool.Dequeue();
            } while ( false );
            Monitor.Exit( m_LockFreePool );

            return l_Return;
        }

        /// <summary>
        /// 内存池释放数据
        /// </summary>
        /// <param name="TContent"></param>
        public void ReleasePoolContent( T TContent )
        {
            if ( TContent == null )
                return;

            Monitor.Enter( m_LockFreePool );
            {
                m_FreePool.Enqueue( TContent );
            }
            Monitor.Exit( m_LockFreePool );
        }

        /// <summary>
        /// 释放内存池内全部的数据
        /// </summary>
        public void Free()
        {
            Monitor.Enter( m_LockFreePool );
            {
                m_FreePool.Clear();
            }
            Monitor.Exit( m_LockFreePool );
        }

        /// <summary>
        /// 给出内存池的详细信息
        /// </summary>
        /// <param name="strName"></param>
        /// <param name="iFreeCount"></param>
        /// <param name="iInitialCapacity"></param>
        /// <param name="iCurrentCapacity"></param>
        /// <param name="iMisses"></param>
        public void GetInfo( out string strName, out int iFreeCount, out int iInitialCapacity, out int iCurrentCapacity, out int iMisses )
        {
            // 可以不需要锁定的，因为只是给出没有修改数据
            strName = m_Name;
            iFreeCount = m_FreePool.Count;
            iInitialCapacity = m_InitialCapacity;
            iCurrentCapacity = m_InitialCapacity * ( 1 + m_Misses ); // m_Misses是从零开始计算的因此需加1
            iMisses = m_Misses;
        }
        #endregion
    }
}
#endregion

